package controller;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;

import model.SearchEvent;
import view.HomeCalendar;

public class ActionHomeCalendar  implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		String action = ((Component) e.getSource()).getName();
		String num = e.getActionCommand();

		System.out.println("action: "+num);
		System.out.println("name: "+action);

		switch (action){
		case "monthChoice":{
			int i = HomeCalendar.monthChoice.getSelectedIndex();
			//System.out.println("mese: "+i);
			if (i >= 0) {
				HomeCalendar.mm = i;
				System.out.println("mese calendario: "+HomeCalendar.mm);
				//System.out.println("Month=" + mm);
				HomeCalendarObserver.obs.uppdater();
			}break;
		}
		case "yearChoice":{
			String year = (String) HomeCalendar.yearChoice.getSelectedItem();
			System.out.println("Year=" + year);
			int i = HomeCalendar.yearChoice.getSelectedIndex();
			if (i >= 0) {
				HomeCalendar.yy = Integer.parseInt(HomeCalendar.yearChoice.getSelectedItem().toString());
				//System.out.println("Year=" + yy);
				HomeCalendarObserver.obs.uppdater();
			}
			else{
				HomeCalendar.yy = Integer.parseInt(year);
				//System.out.println("Year=" + yy);
				HomeCalendarObserver.obs.uppdater();
			}

			break;
		}
		case "dayChoice":{
			if (!num.equals("")){
				// set the current day highlighted
				HomeCalendar.setDayActive(Integer.parseInt(num));
				SearchEvent.evn.uppdater();
			}
			break;
		}
		case "plusYear":{
			HomeCalendar.yy++;
			HomeCalendarObserver.obs.uppdater();
			break;
		}
		case "plusMonth":{
			if (HomeCalendar.mm == 11){
				HomeCalendar.mm = 0;
				HomeCalendar.yy++;
			}
			else
				HomeCalendar.mm++;
			HomeCalendarObserver.obs.uppdater();
			break;
		}
		case "minusYear":{
			HomeCalendar.yy--;
			HomeCalendarObserver.obs.uppdater();
			break;
		}
		case "minusMonth":{
			if (HomeCalendar.mm == 0){
				HomeCalendar.mm = 11;
				HomeCalendar.yy--;
			}
			else
				HomeCalendar.mm--;
			HomeCalendarObserver.obs.uppdater();
			break;

		}
		}
		((JComponent)e.getSource()).requestFocus();
	}

}


